import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;


public class FileInfo {
    /**
     * 元数据获取方法
     * @param url 服务器地址
     * @param uuid uuid（唯一标识符）
     * @return 元数据的json
     * @throws Exception
     */
    public  JSONObject getInfo(String url,String uuid) throws Exception {
        url = url + "?uuid=" + uuid;
        URL connectionUrl = new URL(url);
        URLConnection urlConnection = connectionUrl.openConnection();
        HttpURLConnection connection = (HttpURLConnection) urlConnection;
        //将该连接的输入输出都设置为允许
        connection.setDoInput(true);
        connection.setDoOutput(true);

        connection.setUseCaches(false);
        connection.setRequestMethod("GET");

        //长连接,建议服务器端保留连接，这样下次向同一个服务器发请求时可以走同一个连接
        connection.setRequestProperty("Connection", "Keep-Alive");
        //设置请求编码格式
        connection.setRequestProperty("Charset", "UTF-8");
        //连接阶段的设置结束

        InputStream is= connection.getInputStream();//以输入流的形式返回
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        byte [] buffer=new byte[2048];
        int length=0;
        while((length=is.read(buffer))!=-1){
            baos.write(buffer, 0, length);
        }
        String jsonString=baos.toString();
        baos.close();
        is.close();
        JSONObject jsonObject= JSON.parseObject(jsonString);
        return jsonObject;

    }
}
